<?php
declare (strict_types = 1);
namespace app\user\logic\user;

use think\facade\Event;
use app\common\exception\LogicException;
use app\user\model\user\BalanceFlow as BalanceFlowModel;
use app\user\event\subscribes\user\Balance as BalanceSubscribe;


/**
 * 账户余额流水接口类
 */
class BalanceFlow extends BalanceFlowModel
{
    
    /**
     * 流水记录
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-21
     * @return   [type]                      [description]
     */
    public function updateFlow($uid, $alter_type, $num, $balance, $rel_type, $rel_pk, $note = '')
    {
        $data = [
            'user_id'       => $uid,
            'alter_type'    => $alter_type,
            'num'           => $num,
            'balance'       => $balance,
            'rel_type'      => $rel_type,
            'rel_pk'        => $rel_pk,
            'note'          => $note,
        ];

        $re = $this::create($data);
        if ( $re->isEmpty() ) throw new LogicException('流水添加失败');
        // 流水添加后处理事件 
        Event::subscribe(BalanceSubscribe::class);
        Event::trigger('BalanceUpdata', $data);

        return $this;
        
    }

    /**
     * 用户余额明细
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-20
     */
    public function flow()
    {
        $data = $this
            ->field('alter_type,num,create_time,note')
            ->scope('mid')
            ->order('create_time', 'DESC')
            ->paginate($this->getListRows())
            ;

        return $data;
    }


}
